﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reconciliation.Business
{
    public class Order
    {        
        public static List<Domain.Order> GetOrderList(int invoiceID)
        {
            try
            {
                List<Domain.Order> domainOrderList = Data.Order.GetOrderList(invoiceID);
                BusinessCacheManager.AddToCache(BusinessCacheManager.Key.OrderListByInvoice.ToString(), domainOrderList);
                foreach (Domain.Order o in domainOrderList)
                {
                    foreach (Domain.OrderDetail od in o.OrdreDetailList)
                    {
                        od.Difference = od.ExpectedReturn - od.PhysicalReturn;
                    }
                    o.CitDifference = o.OrdreDetailList.Sum(s => s.Difference);
                    o.CitPhysicalReturn = o.OrdreDetailList.Sum(s => s.PhysicalReturn);
                    o.CitExpectedReturn = o.OrdreDetailList.Sum(s => s.ExpectedReturn);
                    o.ClientDifference = o.OrdreDetailList.Sum(s => s.ClientDifference);
                }
                return domainOrderList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.Order> GetOrderList(DateTime startDate, DateTime endDate)
        {
            try
            {
                List<Domain.Order> domainOrderList = Data.Order.GetOrderList(startDate, endDate);
                BusinessCacheManager.AddToCache(BusinessCacheManager.Key.OrderListByDate.ToString(), domainOrderList);
                foreach (Domain.Order o in domainOrderList)
                {
                    foreach (Domain.OrderDetail od in o.OrdreDetailList)
                    {
                        od.Difference = od.ExpectedReturn - od.PhysicalReturn;
                    }
                    o.CitDifference = o.OrdreDetailList.Sum(s => s.Difference);
                    o.CitPhysicalReturn = o.OrdreDetailList.Sum(s => s.PhysicalReturn);
                    o.CitExpectedReturn = o.OrdreDetailList.Sum(s => s.ExpectedReturn);
                    o.ClientDifference = o.OrdreDetailList.Sum(s => s.ClientDifference);
                }
                return domainOrderList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static Domain.Order GetOrder(int orderID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                Domain.Order domainOrder = null;
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.OrderListByInvoice.ToString());
                if (domainOrderList != null)
                {
                    domainOrder = domainOrderList.Where(o => o.OrderID.Equals(orderID)).FirstOrDefault();
                }
                if (domainOrder == null)
                {
                    domainOrder = Data.Order.GetOrder(orderID);
                    //Calculating Difference
                    foreach (Domain.OrderDetail od in domainOrder.OrdreDetailList)
                    {
                        od.ClientDifference = od.ExpectedReturn - od.PhysicalReturn;
                    }
                }
                return domainOrder;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.Order> GetGeneratedOrder()
        {
            try
            {
                List<Domain.Order> domainOrderList = Data.Order.GetGeneratedOrderList();
                BusinessCacheManager.AddToCache(BusinessCacheManager.Key.NewOrderList.ToString(), domainOrderList);
                return domainOrderList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.OrderDetail> GetGeneratedOrderDetail(int orderID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                Domain.Order domainOrder = null;
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.NewOrderList.ToString());
                if (domainOrderList != null)
                {
                    domainOrder = domainOrderList.Where(o => o.OrderID.Equals(orderID)).FirstOrDefault();
                }
                if (domainOrder == null)
                {
                    domainOrder = Data.Order.GetOrder(orderID);
                }
                return domainOrder.OrdreDetailList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.OrderDetail> GetOrderDetail(int orderID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                Domain.Order domainOrder = null;
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.OrderListByInvoice.ToString());
                if (domainOrderList != null)
                {
                    domainOrder = domainOrderList.Where(o => o.OrderID.Equals(orderID)).FirstOrDefault();
                }
                if (domainOrder == null)
                {
                    domainOrder = Data.Order.GetOrder(orderID);
                    //Calculating Difference
                    foreach (Domain.OrderDetail od in domainOrder.OrdreDetailList)
                    {
                        od.ClientDifference = od.ExpectedReturn - od.PhysicalReturn;
                    }
                }
                return domainOrder.OrdreDetailList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static void SetPackagingOrder(Domain.Order domainOrder)
        {
            try
            {
                domainOrder.Status = Enum.GetName(typeof(Domain.Global.OrderCycle), Domain.Global.OrderCycle.Packed);
                Data.Order.SetPackagingOrderDetail(domainOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void SetUnPackagingOrder(Domain.Order domainOrder)
        {
            try
            {
                domainOrder.Status = Enum.GetName(typeof(Domain.Global.OrderCycle), Domain.Global.OrderCycle.UnPacked);
                Data.Order.SetUnPackagingOrderDetail(domainOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Domain.OrderCycle> GetOrderCycle(int orderID)
        {
            try
            {
                return Data.OrderCycle.GetOrderCycle(orderID);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }
    }
}
